package cn.lwy.example.niit_race.controller;

import cn.lwy.example.niit_race.pojo.Matter;
import cn.lwy.example.niit_race.service.MatterService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;

@RestController
public class ServeAPI {
    @Autowired
    MatterService matterService;

    //获取当前用户所有事件
    @RequestMapping("/getMatter")
    public String getMatterByUserId() throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByUserId(matterService.getIdByUsername(getUserName())));
    }

    //更新事件
    @RequestMapping("/update")
    public void updateMatter(Matter matter){
        //时间格式：yyyy-MM-dd HH:mm:ss
        matterService.updateMatter(matter);
    }

    //添加事件
    @RequestMapping("/add")
    public void addMatter(Matter matter){
        matter.setAuthorId(matterService.getIdByUsername(getUserName()));
        matter.setFinish(0);
        matter.setRm(0);
        matter.setSign(0);
        matterService.addMatter(matter);
    }

    //删除事件
    @RequestMapping("/del/{id}")
    public void deleteMatter(@PathVariable("id")int id){
        matterService.delMatter(id);
    }

    //根据截止日期查找当前用户事件
    @RequestMapping("/deadline/{date}")
    public String getMatterByDeadline(@PathVariable("date") Date date) throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByDeadline(date,
                matterService.getIdByUsername(getUserName())));
    }

    //查看当前用户垃圾箱中的事件
    @RequestMapping("/rm")
    public String getMatterByRM() throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByRM(matterService.getIdByUsername(getUserName())));
    }

    //将事件加入垃圾箱
    @RequestMapping("/toRM/{id}")
    public void goToRMById(@PathVariable("id")int id){
        matterService.goToRMById(id);
    }

    //将事件移除垃圾箱
    @RequestMapping("/canleRM/{id}")
    public void rollbackMatterRMById(@PathVariable("id")int id){
        matterService.rollbackMatterRMById(id);
    }

    //查找当前用户已完成的事件
    @RequestMapping("/finish")
    public String getMatterByIsFinish() throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByIsFinish(matterService.getIdByUsername(getUserName())));
    }

    //查找当前用户未完成的事件
    @RequestMapping("/nofinish")
    public String getMatterByNoFinish() throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByNoFinish(matterService.getIdByUsername(getUserName())));
    }

    //查找当前type为每日打卡且未打卡的事件
    @RequestMapping("/noSign")
    public String getMatterByNoSign() throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByNoSign(matterService.getIdByUsername(getUserName())));
    }

    //查找已打卡的事件
    @RequestMapping("/sign")
    public String getMatterByIsSing() throws JsonProcessingException {
        return getJsonStr(matterService.getMatterByIsSign(matterService.getIdByUsername(getUserName())));
    }

    private String getUserName(){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        User user = (User) auth.getPrincipal();
        String name = user.getUsername().toString();
        return name;
    }

    private String getJsonStr(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String str = mapper.writeValueAsString(obj);
        return str;
    }
}
